package com.zqu.service.impl;

import com.zqu.mapper.UserMapper;
import com.zqu.pojo.User;
import com.zqu.service.UserService;
import com.zqu.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    User user;
    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public User selectUserById(long id) {
            String key = id+"";
            if(redisUtil.hasKey(key)) {
                user = (User)redisUtil.get(key);
                System.out.println("查询的是缓存");
            }else{
                user = userMapper.selectTById(id);
                System.out.println("查询的是数据库");
                System.out.println(redisUtil.set(key,user) ? "插入成功" : "插入失败");
            }
            return user;

    }

    @Override
    public List<User> selectList(User user) {
        return userMapper.selectList(user);
    }

    @Override
    public boolean insertUser(User user) {
        return userMapper.insertT(user)>0;
    }

    @Override
    public boolean updateUser(User user) {
        return userMapper.updateT(user)>0;
    }

    @Override
    public boolean deleteUser(long id) {
        return userMapper.deleteT(id)>0;
    }

    @Override

    public boolean deleteUserByIds(String[] ids) {
        return userMapper.deleteTByIds(ids) > 0;
    }

    @Override
    public User selectUser(User user) {
        return userMapper.selectUser(user);
    }
}
